package com.anolesoft.PlanOrder.util;
import com.anolesoft.PlanOrder.service.FengCunFangHuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Component
public class BarCodeGenerate {
    @Autowired
     FengCunFangHuService fengCunFangHuService;

    public  String getBarCoede(String factoryID){
        return getBarCodeByFactory(factoryID);
    }

    public  String getBarCodeByFactory(String factoryID){

        String code = "";
        int count = 0;
        count=fengCunFangHuService.barCodeGenerate(factoryID);
        code = getTM2(count,factoryID) + check((count ) + "");
        return code;
    }

    public  String getTM2(int number,String factoryID){
        String date = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
        if(factoryID.equals("46")){
            factoryID = "46";
        }
        if(factoryID.length()==1){
            return "0"+factoryID+date+String.format("%1$05d",number);
        }
        return factoryID+date+String.format("%1$05d",number);
    }

    public  int check(String xuhao){
        char[] b = xuhao.toCharArray();
        int i =0;
        int j =0;
        for(int x= 0 ;x<b.length;x++){
            if(x%2==0){
                i=Integer.parseInt(b[x]+"")+i;
            }else if(x%2!=0){
                j=Integer.parseInt(b[x]+"")+j;
            }
        }
        int y = i*3+j;
        int e = (y/10+1)*10-y+2;
        return e%10;
    }
}
